<?php

require_once('request/AbstractRequest.class.php');



class FetchCategoriesRequest extends AbstractRequest {
	
	private $_out = '';
	
	/**
	 * Core fetch method.
	 *
	 * @param SimpleXML $requestXml not used by fetches
	 */
	protected final function internalExecute($requestXml) {
		if(false) $requestXml; // Zend Studio hack to get rid of warning
		
		$this->out('<Categories>');
		
		$this->processAnyCategories(true, $this->categoryDao->fetchOfferedCategories());
		$this->processAnyCategories(false, $this->categoryDao->fetchNeededCategories());
		

		$this->out('</Categories>');
		
		echo $this->_out;
	}
	
	/**
	 * X.
	 *
	 * @param boolean $isOffered
	 * @param Category[] $categories
	 */
	private function processAnyCategories($isOffered, $categories) {
		$tagName = $isOffered ? 'OfferedCategories' : 'NeededCategories';
		$this->out('<'.$tagName.'>');
		foreach($categories as $category) {
			$sub_category_id = $category->subCategoryId;
			$title = $category->title;
			
			$this->out('<Category>');
			$this->out('<CategoryId>' . $sub_category_id . '</CategoryId>');
			$this->out('<Title>' . wrapCData($title) . '</Title>');
			$this->out('</Category>');
		}
		$this->out('</'.$tagName.'>');
	}
	
	private function out($text) {
		$this->_out .= $text . "\n";
	}
	
}

?>